package com.zf.yichat.api.controller.wx;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zf.yichat.api.controller.BaseController;
import com.zf.yichat.api.dto.request.WxUserInfoRequest;
import com.zf.yichat.api.dto.resp.WxUserInfoDto;
import com.zf.yichat.service.config.RedisService;
import com.zf.yichat.utils.YiChatMsgCode;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import com.zf.yichat.utils.weixin.AccessToken;
import com.zf.yichat.utils.weixin.MyWeixinUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Objects;

/**
 * 一句话描述功能
 *
 * @author fengsong
 * @date create in 09:03 2020/3/23 2020
 */
@RestController
@RequestMapping("wx")
public class WxController extends BaseController {
    private final static String API_ACCESS_TOKEN_KEY = "wx_jsapi_access_token_key";
    private final static String JSAPI_TICKET_KEY = "wx_jsapi_ticket_key";
    @Autowired
    RedisService redisService;

    @PostMapping("user/info")
    public FsResponse getUserInfo(@RequestBody WxUserInfoRequest request) {

        if (StringUtils.isBlank(request.getCode())) {
            return FsResponseGen.success(YiChatMsgCode.SYSTEM_PARAM_ERROR);
        }

        String infoRes = MyWeixinUtil.getUserInfo(request.getCode());

        JSONObject jb = JSON.parseObject(infoRes);
        WxUserInfoDto dto = new WxUserInfoDto();
        dto.setAvatar(jb.getString("headimgurl"));
        dto.setCity(jb.getString("city"));
        dto.setProvince(jb.getString("province"));
        dto.setNick(jb.getString("nickname"));
        dto.setOpenId(jb.getString("openid"));
        dto.setUnionId(jb.getString("unionid"));
        dto.setSex(jb.getInteger("sex"));
        return FsResponseGen.successData(dto);
    }

    /**
     * 获取微信签名
     */
    @PostMapping("signature")
    public FsResponse WapSignSignatureAction(@RequestBody WxUserInfoRequest request) {
        String ticket = redisService.getVal(JSAPI_TICKET_KEY);
        if (StringUtils.isEmpty(ticket)) {
            //由于ticket和token过期时间相同，生命周期相同，所以此处无需判断token是否过期,也无需缓存（如果其他地方用到则需要缓存）
            AccessToken wxAccessToken = MyWeixinUtil.getAccessToken();
            if (Objects.isNull(wxAccessToken)) {
                return FsResponseGen.failMsg("accessToken获取失败");
            }
            String accessToken = wxAccessToken.getToken();
            //微信给的过期时间是7200秒
//            redisService.setVal(API_ACCESS_TOKEN_KEY, accessToken, 7200);
            //微信ticket缓存时间也是7200秒
            ticket = MyWeixinUtil.getTicket(accessToken);
            redisService.setVal(JSAPI_TICKET_KEY, ticket, 7200);
        }
//        Map<String, String> jsData = MyWeixinUtil.sign(ticket, redisService.getVal("test_wxjs_token"));
        Map<String, String> jsData = MyWeixinUtil.sign(ticket, request.getCode());
        return FsResponseGen.successData(jsData);
    }

}
